#!/bin/bash

# 创建镜像存储目录
EXPORT_DIR="./exported_images"
mkdir -p "$EXPORT_DIR"

# 列出所有镜像
images=$(docker images | awk '{print $1 ":" $2}'|grep -v 'TAG')

# 逐个导出镜像
for image in $images; do
  # 将镜像名称中的 "/" 替换为 "-"
  sanitized_image_name=$(echo "$image" | tr '/' '-'| tr ':' '-')
  exported_images="$EXPORT_DIR/${sanitized_image_name}.tar"

  # 导出镜像
  echo "正在导出镜像 $image 到 exported_images 文件夹 ..."
  docker save "$image" -o "$exported_images"
  if [ $? -eq 0 ]; then
    echo "镜像 $image 导出成功！"
  else
    echo "镜像 $image 导出失败！"
  fi
done